reverseArray
Arrays Sandbox PanosP Revised

Reversing an Array

Problem
=======

Given an array, reverse the order of the elements, assuming
that array is number indexed with step of one.

Solution
========

Synopsis
--------

  reverseArray(arr	[, from [, to]])

Reverse the order of elements in a number indexed array.
Array is passed as the first argument to the function.
Array limits (the lower and higher indices) are passed
as second and third arguments. If not passed (empty)
the second argument, assume the lower index to be zero.
If not passed (empty) the third argument, assume the
high index to be the last index found in the array
steping one by one _after_ the low index.
If any of the indices passed is invalid or out
of range, then the function prints a message an exits
with a non zero exit status.

Code
----

 function reverseArray(arr, from, to,		errs, x, count) {
 	if (from == "")
 		from = 0
 	else if (from ~ /^[ \t]*[0-9]+[ \t]*$/)
 		from += 0
 	else
 		errs += warning("reverseArray: invalid `from' " \
			"index (" from ")")
 	if (to ~ /^[ \t]*[0-9]+[ \t]*$/)
 		to += 0
 	else if (to != "")
 		errs += warning("reverseArray: invalid `to' " \
			"index (" to ")")
 	notOk(errs)
 	if (!(from in arr))
 		errs += barph("reverseArray: `from' " \
			"index out of range (" from ")")
 	# If not given the upper index, the seek it stepping one
 	# by one after the low index. If there are gaps in the
 	# array (some indices are missing) the the elements after
 	# the first gap will be ignored!
 	if (to == "") {
 		for (to = from + 1; to in arr; to++)
 			;
 		to--
 	}
 	# Normally, this is the point to check for the existence
 	# of the `to' index. But we leave that free in order to
 	# give the flexibility to create new elements as needed,
 	# e.g. given the array a[1], a[2], a[3] we can call
 	# reverseArray(a, 1, 10) and produce a[1]...a[10], puting
 	# a[1] to a[10], a[2] to a[9] and a[3] to a[8].
 	if (to < from)
 		errs += barph("reverseArray: invalid index range (" \
 			from " - " to ")")
 	for (count = 0; from < to; count++) {
 		x = arr[from]
 		arr[from] = arr[to]
 		arr[to] = x
 		from++
 		to--
 	}
 	return(count)
 }

Return Value
------------

The function returns the number of switched elements.

Discussion
==========

The function gives a lot of freedom in the calling phace.
However, the most frequently form of calling the function
may be the:

  reverseArray(x, 1)

or something like that. I believe it's a good practice to
give both the lower and upper indices, but that's a matter
of taste. Another point to ponder:
Given the upper index, missing elements will
be created!

Of course, the indices given may reverse only part
of the array, e.g.

 function TestRevArr(			a, i) {
 	# Create an array of 20 elements indexed zero
 	# based.
 	for (i = 0; i < 10; i++)
 		a[i] = "element_" i
 	# Reverse only the elements 3 throuh 7.
 	reverseArray(a, 3, 7)
 	for (i = 0; i < 10; i++)
 		print i ": " a[i]
 }

will print:

  0: element_0
  1: element_1
  2: element_2
  7: element_7
  6: element_6
  5: element_5
  4: element_4
  3: element_3
  8: element_8
  9: element_9

Author
======

Panos Papadopoulos

